//
//  XKDrawMoneyView.m
//  xk100
//
//  Created by xiaokang on 2017/5/27.
//  Copyright © 2017年 xiaokang. All rights reserved.
//



#import "XKDrawMoneyView.h"
#import "XKBankDataModel.h"
@interface XKDrawMoneyView () <UITextFieldDelegate>

@property (nonatomic, strong) UILabel       *drawMoneyMindLabel;
@property (nonatomic, strong) UIView        *drawMoneyBackgroundView;
@property (nonatomic, strong) UILabel       *drawMoneyFlagLabel;
@property (nonatomic, strong) UITextField   *drawMoneyTextField;
@property (nonatomic, strong) UILabel       *drawMoneyActualTitleLabel;
@property (nonatomic, strong) UILabel       *drawMoneyActualCountLabel;
@property (nonatomic, strong) UILabel       *drawMoneyFeeTitleLabel;
@property (nonatomic, strong) UILabel       *drawMoneyFeeLabel;
@property (nonatomic, strong) UILabel       *drawMoneyFeePercentLabel;
@property (nonatomic, strong) UIView        *drawMoneyGrayLineView;
@property (nonatomic, strong) UILabel       *availableBalanceTitleLabel;
@property (nonatomic, strong) UILabel       *availableBalanceCountLabel;
@property (nonatomic, strong) UILabel       *availableBalanceFlagLabel;
@property (nonatomic, strong) UIButton      *drawMoneyAllMoneyDrawButton;

@property (nonatomic, strong) UILabel       *selectCardMindLabel;
@property (nonatomic, strong) UIView        *selectCardBackgroundView;
@property (nonatomic, strong) UIImageView   *selectCardIconImageView;
@property (nonatomic, strong) UILabel       *selectCardTitleLabel;
@property (nonatomic, strong) UILabel       *selectCardNumberTitleLabel;
@property (nonatomic, strong) UILabel       *selectCardNumberCountLabel;
@property (nonatomic, strong) UIImageView   *selectCardArrowImageView;

@property (nonatomic, strong) UIButton      *drawMoneyConfirmButton;

@end
@implementation XKDrawMoneyView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kBaseGrayColor;
        [self initSubviews];
    }
    return self;
}

#pragma mark -
- (void)initSubviews {
    _drawMoneyBackgroundView    = [UIView new];
    _drawMoneyBackgroundView.backgroundColor = [UIColor whiteColor];
    
    _drawMoneyMindLabel         = [UILabel getLabWithText:@"金额:(单笔限额100-50,000元,单日最多10次)"
                                                 textFont:Font_14
                                                textColor:[UIColor grayColor]
                                            textAlignment:NSTextAlignmentLeft];
    _drawMoneyFlagLabel         = [UILabel getLabWithText:@"¥:"
                                                 textFont:Font_26
                                                textColor:[UIColor darkGrayColor]
                                            textAlignment:NSTextAlignmentLeft];
    _drawMoneyActualTitleLabel  = [UILabel getLabWithText:@"实际到账金额"
                                                 textFont:Font_12
                                                textColor:[UIColor darkGrayColor]
                                            textAlignment:NSTextAlignmentRight];
    _drawMoneyActualCountLabel  = [UILabel getLabWithText:@"0.00"
                                                 textFont:Font_13
                                                textColor:[UIColor blackColor]
                                            textAlignment:NSTextAlignmentRight];
    _drawMoneyFeeTitleLabel     = [UILabel getLabWithText:@"费率:"
                                                 textFont:Font_12
                                                textColor:[UIColor darkGrayColor]
                                            textAlignment:NSTextAlignmentRight];
    _drawMoneyFeeLabel          = [UILabel getLabWithText:@"0"
                                                 textFont:Font_12
                                                textColor:[UIColor darkGrayColor]
                                            textAlignment:NSTextAlignmentRight];
    _drawMoneyFeePercentLabel   = [UILabel getLabWithText:@"%"
                                                 textFont:Font_12
                                                textColor:[UIColor darkGrayColor]
                                            textAlignment:NSTextAlignmentRight];
    _availableBalanceTitleLabel = [UILabel getLabWithText:@"可用余额:"
                                                 textFont:Font_13
                                                textColor:[UIColor darkGrayColor]
                                            textAlignment:NSTextAlignmentLeft];
    _availableBalanceCountLabel = [UILabel getLabWithText:@"0.00"
                                                 textFont:Font_13
                                                textColor:[UIColor darkGrayColor]
                                            textAlignment:NSTextAlignmentLeft];
    _availableBalanceFlagLabel  = [UILabel getLabWithText:@"元"
                                                 textFont:Font_13
                                                textColor:[UIColor darkGrayColor]
                                            textAlignment:NSTextAlignmentLeft];
    
    _drawMoneyTextField = [[UITextField alloc] init];
    _drawMoneyTextField.keyboardType    = UIKeyboardTypeDecimalPad;
    _drawMoneyTextField.borderStyle     = UITextBorderStyleNone;
    _drawMoneyTextField.textColor       = [UIColor blackColor];
    _drawMoneyTextField.delegate        = self;
    _drawMoneyTextField.font            = Font(32);
    [_drawMoneyTextField addTarget:self action:@selector(textFieldDidEdited:) forControlEvents:UIControlEventEditingChanged];
    
    _drawMoneyGrayLineView = [UIView new];
    _drawMoneyGrayLineView.backgroundColor  = kBaseGrayColor;
    
    _drawMoneyAllMoneyDrawButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _drawMoneyAllMoneyDrawButton.titleLabel.font = Font(13);
    [_drawMoneyAllMoneyDrawButton setTitle:@"全部提现" forState:UIControlStateNormal];
    [_drawMoneyAllMoneyDrawButton addTarget:self
                                     action:@selector(allMoneyDrawButtonClickEvent:)
                           forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:_drawMoneyMindLabel];
    [self addSubview:_drawMoneyBackgroundView];
    [_drawMoneyBackgroundView addSubview:_drawMoneyFlagLabel];
    [_drawMoneyBackgroundView addSubview:_drawMoneyTextField];
    [_drawMoneyBackgroundView addSubview:_drawMoneyActualTitleLabel];
    [_drawMoneyBackgroundView addSubview:_drawMoneyActualCountLabel];
    [_drawMoneyBackgroundView addSubview:_drawMoneyFeeTitleLabel];
    [_drawMoneyBackgroundView addSubview:_drawMoneyFeeLabel];
    [_drawMoneyBackgroundView addSubview:_drawMoneyFeePercentLabel];
    [_drawMoneyBackgroundView addSubview:_drawMoneyGrayLineView];
    [_drawMoneyBackgroundView addSubview:_availableBalanceTitleLabel];
    [_drawMoneyBackgroundView addSubview:_availableBalanceCountLabel];
    [_drawMoneyBackgroundView addSubview:_availableBalanceFlagLabel];
    [_drawMoneyBackgroundView addSubview:_drawMoneyAllMoneyDrawButton];
    
    
    [_drawMoneyMindLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(20*Scale_W);
    }];
    
    [_drawMoneyBackgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(_drawMoneyMindLabel.mas_bottom).offset(4*Scale_W);
    }];
    
    [_drawMoneyFlagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_drawMoneyBackgroundView).offset(20*Scale_W);
        make.height.mas_equalTo(68*Scale_W);
        make.width.mas_equalTo(30*Scale_W);
        make.top.mas_equalTo(_drawMoneyBackgroundView);
    }];
    
    [_drawMoneyTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_drawMoneyFlagLabel.mas_right);
        make.right.mas_equalTo(_drawMoneyBackgroundView).offset(-100*Scale_W);
        make.centerY.height.mas_equalTo(_drawMoneyFlagLabel);
    }];
    
    [_drawMoneyActualTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_drawMoneyBackgroundView).offset(-20*Scale_W);
        make.top.mas_equalTo(_drawMoneyBackgroundView).offset(8*Scale_W);
    }];
    
    [_drawMoneyActualCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_drawMoneyActualTitleLabel);
        make.centerY.mas_equalTo(_drawMoneyFlagLabel);
    }];
    
    [_drawMoneyFeePercentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_drawMoneyActualTitleLabel);
        make.bottom.mas_equalTo(_drawMoneyTextField).offset(-8*Scale_W);
    }];
    
    [_drawMoneyFeeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_drawMoneyFeePercentLabel.mas_left);
        make.bottom.mas_equalTo(_drawMoneyFeePercentLabel);
    }];
    
    [_drawMoneyFeeTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_drawMoneyFeeLabel.mas_left);
        make.bottom.mas_equalTo(_drawMoneyFeePercentLabel);
    }];
    
    [_drawMoneyGrayLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_drawMoneyFlagLabel);
        make.right.mas_equalTo(_drawMoneyActualTitleLabel);
        make.top.mas_equalTo(_drawMoneyTextField.mas_bottom);
        make.height.mas_equalTo(1*Scale_W);
    }];
    
    [_availableBalanceTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_drawMoneyFlagLabel);
        make.top.mas_equalTo(_drawMoneyGrayLineView.mas_bottom).offset(8*Scale_W);
        make.bottom.mas_equalTo(_drawMoneyBackgroundView).offset(-8*Scale_W).priorityHigh();
    }];
    
    [_availableBalanceCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_availableBalanceTitleLabel.mas_right);
        make.centerY.mas_equalTo(_availableBalanceTitleLabel);
    }];
    
    [_availableBalanceFlagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_availableBalanceCountLabel.mas_right);
        make.centerY.mas_equalTo(_availableBalanceTitleLabel);
    }];
    
    [_drawMoneyAllMoneyDrawButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_drawMoneyActualTitleLabel);
        make.centerY.mas_equalTo(_availableBalanceTitleLabel);
    }];
    
    /**********************************************************************************************/
    _selectCardMindLabel        = [UILabel getLabWithText:@"到账银行卡"
                                                 textFont:Font_14
                                                textColor:[UIColor grayColor]
                                            textAlignment:NSTextAlignmentLeft];
    _selectCardBackgroundView   = [UIView new];
    _selectCardBackgroundView.backgroundColor = [UIColor whiteColor];
    
    _selectCardIconImageView    = [UIImageView new];
    _selectCardIconImageView.image = [UIImage imageNamed:@"bank_card_placeholder"];
    _selectCardTitleLabel       = [UILabel getLabWithText:@"您尚未选择银行卡(点击选择)"
                                                 textFont:Font_13
                                                textColor:[UIColor darkGrayColor]
                                            textAlignment:NSTextAlignmentLeft];
    _selectCardNumberTitleLabel = [UILabel getLabWithText:@"尾号"
                                                 textFont:Font_13
                                                textColor:[UIColor darkGrayColor]
                                            textAlignment:NSTextAlignmentLeft];
    _selectCardNumberCountLabel = [UILabel getLabWithText:@"****"
                                                 textFont:Font_13
                                                textColor:[UIColor darkGrayColor]
                                            textAlignment:NSTextAlignmentLeft];
    _selectCardArrowImageView   = [UIImageView new];
    _selectCardArrowImageView.image = [UIImage imageNamed:@"my_con_icon_next"];
    
    [self addSubview:_selectCardMindLabel];
    [self addSubview:_selectCardBackgroundView];
    [_selectCardBackgroundView addSubview:_selectCardIconImageView];
    [_selectCardBackgroundView addSubview:_selectCardTitleLabel];
    [_selectCardBackgroundView addSubview:_selectCardNumberTitleLabel];
    [_selectCardBackgroundView addSubview:_selectCardNumberCountLabel];
    [_selectCardBackgroundView addSubview:_selectCardArrowImageView];
    
    UITapGestureRecognizer *selectTapGestureRecognizer = [[UITapGestureRecognizer alloc]
                                                          initWithTarget:self
                                                          action:@selector(cardSelectTapEvent:)];
    [_selectCardBackgroundView addGestureRecognizer:selectTapGestureRecognizer];
    
    
    
    [_selectCardMindLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_drawMoneyMindLabel);
        make.top.mas_equalTo(_drawMoneyBackgroundView.mas_bottom).offset(20*Scale_W);
    }];
    
    [_selectCardBackgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(_selectCardMindLabel.mas_bottom).offset(4*Scale_W);
    }];
    
    [_selectCardIconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_selectCardBackgroundView).offset(20*Scale_W);
        make.top.mas_equalTo(_selectCardBackgroundView).offset(8*Scale_W);
        make.size.mas_equalTo(CGSizeMake(40*Scale_W, 40*Scale_W));
        make.bottom.mas_equalTo(_selectCardBackgroundView).offset(-8*Scale_W).priorityHigh();
    }];
    
    [_selectCardTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_selectCardIconImageView.mas_right).offset(8*Scale_W);
        make.top.mas_equalTo(_selectCardIconImageView);
    }];
    
    [_selectCardNumberTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_selectCardTitleLabel);
        make.bottom.mas_equalTo(_selectCardIconImageView);
    }];
    
    [_selectCardNumberCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_selectCardNumberTitleLabel.mas_right);
        make.bottom.mas_equalTo(_selectCardNumberTitleLabel);
    }];
    
    [_selectCardArrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_selectCardBackgroundView).offset(-20*Scale_W);
        make.centerY.mas_equalTo(_selectCardIconImageView);
        make.size.mas_equalTo(CGSizeMake(8*Scale_W, 14*Scale_W));
    }];
    
    /**********************************************************************************************/
    _drawMoneyConfirmButton = [UIButton getClickBtnWithTitle:@"提现"];
    _drawMoneyConfirmButton.enabled = YES;
    [_drawMoneyConfirmButton addTarget:self
                                action:@selector(drawMoneyConfirmButtonClickEvent:)
                      forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_drawMoneyConfirmButton];
    
    [_drawMoneyConfirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_selectCardBackgroundView.mas_bottom).offset(20*Scale_W);
        make.left.mas_equalTo(self).offset(20*Scale_W);
        make.right.mas_equalTo(self).offset(-20*Scale_W);
        make.height.mas_equalTo(40*Scale_W);
    }];
    
}


#pragma mark - 提现
- (void)drawMoneyConfirmButtonClickEvent:(UIButton *)sender {
    if (_DrawMoneyConfirmBlock) {
        _DrawMoneyConfirmBlock(_drawMoneyTextField.text, _cardModel.card_id);
    }
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSMutableString *text = [NSMutableString stringWithString:textField.text];
    [text replaceCharactersInRange:range withString:string];
    
    BOOL isNumber = NO;
    if ([text isTradePrice]) {
        isNumber = YES;
    }
    
    if (text.doubleValue == 0 && text.length > 1 && ![text containsString:@"."]) {
        isNumber = NO;
    }
    
    if (text.doubleValue >=1 && [text hasPrefix:@"0"]) {
        textField.text = string;
        _drawMoneyActualCountLabel.text = [self actualToAccountMoneyCalculateWithWithdrawMoney:string];
        isNumber = NO;
    }
    
    if (text.doubleValue > kMaxAmountOne.doubleValue) {
        isNumber = NO;
    }
    
    return isNumber;
}

- (void)textFieldDidEdited:(UITextField *)textField {
    
    if (textField.text.doubleValue > _availableBalanceCountLabel.text.doubleValue) {
        _drawMoneyTextField.textColor = [UIColor redColor];
    } else {
        _drawMoneyTextField.textColor = [UIColor blackColor];
    }
    _drawMoneyActualCountLabel.text = [self actualToAccountMoneyCalculateWithWithdrawMoney:textField.text];
    _moneyCountString = textField.text;
}

#pragma mark - 全部提现
- (void)allMoneyDrawButtonClickEvent:(UIButton *)sender {
    _drawMoneyTextField.textColor = [UIColor blackColor];
    
    if (_availableBalanceCountLabel.text.doubleValue > kMaxAmountOne.doubleValue) {
        _drawMoneyTextField.text = kMaxAmountOne;
        _drawMoneyActualCountLabel.text = [self actualToAccountMoneyCalculateWithWithdrawMoney:kMaxAmountOne];
    } else {
        _drawMoneyTextField.text = _availableBalanceCountLabel.text;
        _drawMoneyActualCountLabel.text = [self actualToAccountMoneyCalculateWithWithdrawMoney:_availableBalanceCountLabel.text];
    }
    _moneyCountString = _drawMoneyTextField.text;
}

#pragma mark - 选择银行卡
- (void)cardSelectTapEvent:(UITapGestureRecognizer *)tap {
    if (_SelectedBankCardBlock) {
        _SelectedBankCardBlock();
    }
}

- (void)setCardModel:(XKBankDataModel *)cardModel {
    _cardModel = cardModel;
    _selectCardTitleLabel.text = cardModel.bankname;
    _selectCardNumberCountLabel.text = [cardModel.cardnum substringWithRange:NSMakeRange(cardModel.cardnum.length-4, 4)];
    
    //路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"bankcard"ofType:@"plist"];
    //读取
    NSArray *cardArray = [NSArray arrayWithContentsOfFile:path];
    for (NSDictionary *dict in cardArray) {
        if ([dict[@"bank_card_name"] isEqualToString:cardModel.bankname]) {
            _selectCardIconImageView.image = [UIImage imageNamed:dict[@"bank_card_icon"]];
            break;
        } else {
            _selectCardIconImageView.image = [UIImage imageWithColor:WhiteColor];
        }
    }
}

#pragma mark - 计算提现时实际到账金额
- (NSString *)actualToAccountMoneyCalculateWithWithdrawMoney:(NSString *)money {
    NSString *temp1  = [NSString calculateNumberOne:_drawMoneyFeeLabel.text calculateType:CalculateType_Dividing numberTwo:@"100" withMode:NSRoundPlain scale:2];
    NSString *temp2  = [NSString calculateNumberOne:@"1" calculateType:CalculateType_Subtract numberTwo:temp1 withMode:NSRoundPlain scale:2];
    NSString *result = [NSString calculateNumberOne:money calculateType:CalculateType_Multiply numberTwo:temp2 withMode:NSRoundPlain scale:2];
    return [NSString changeNumberFormat:result];
}

- (void)setFeeString:(NSString *)feeString {
    _feeString = feeString;
    _drawMoneyFeeLabel.text = feeString;
}

- (void)setAvailableBalanceString:(NSString *)availableBalanceString {
    _availableBalanceString = availableBalanceString;
    _availableBalanceCountLabel.text = [NSString changeNumberFormat:availableBalanceString];
}

- (void)setMoneyCountString:(NSString *)moneyCountString {
    _moneyCountString = moneyCountString;
    _drawMoneyTextField.text = moneyCountString;
}

- (void)setActualCountSting:(NSString *)actualCountSting {
    _actualCountSting = actualCountSting;
    _drawMoneyActualCountLabel.text = actualCountSting;
}

- (void)setFirstResponder:(BOOL)firstResponder {
    _firstResponder = firstResponder;
    if (firstResponder) {
        [_drawMoneyTextField becomeFirstResponder];
    } else {
        [_drawMoneyTextField resignFirstResponder];
    }
}

@end
